/**
 * @file 工单操作-服务项
 * @author yangjiangming <449654337@qq.com>
 * @date 2018-12-10 20:13:16
 */
layui.extend({
    zmn_validate: 'zmn.validate'
}).define([ 'layer', 'form', 'NP', 'list_modify_common', '$http'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var NP = layui.NP;
    var $http = layui.$http;
    var opCommonUtils = layui.list_modify_common;

    /**
     * 保存改价格
     * @type {string}
     */
    var SAVE_PRICE_URL = '/work/modify/save/price.action';

    /**
     * 定金变化
     * @param val 定金
     */
    function depositAmountInput(val) {
        if (val && !isNumber(val)) {
            layer.msg("请输入正数且最多两位小数");
            $("#depositAmount").val("");
            val = 0
        }

        var depositAmount = (val || 0) * 100;
        var originalAmount = updatePriceVO.originalAmount || 0;
        var discountAmount = updatePriceVO.discountAmount || 0;
        var prepayAmount = updatePriceVO.prepayAmount || 0;

        // 定金 小于 订单金额 - 优惠 - 预付
        var maxDepositAmount = (originalAmount - discountAmount - prepayAmount)
        if (depositAmount > maxDepositAmount) {
            layer.msg("定金需小于" + NP.divide(maxDepositAmount, 100).toFixed(2) + "元");
            $("#depositAmount").val("");
            depositAmount = 0;
        }

        // 工程师收款金额 = 订单金额 - 优惠总额 - 预收款 - 定金
        var masterAmount = originalAmount - discountAmount - prepayAmount - depositAmount;
        $(".master-amount").text(NP.divide(masterAmount, 100).toFixed(2));

        // 处理仅上门
        if (masterAmount <= 3000 && updatePriceVO.clean != 2) {
            $(".d-visit").prop("disabled", false);
        } else {
            $(".d-visit").prop("disabled", true).prop("checked", false);
        }
    }

    /**
     * 进度款
     * @param val 进度款
     */
    function progressAmountInput(value) {
        var val = value.val();
        if (val && !isNumber(val)) {
            layer.msg("请输入正数且最多两位小数");
            value.val("");
            val = 0
        }

        var amount = (val || 0) * 100;
        var depositAmount = updatePriceVO.depositAmount || 0;
        var originalAmount = updatePriceVO.originalAmount || 0;
        var discountAmount = updatePriceVO.discountAmount || 0;
        var prepayAmount = updatePriceVO.prepayAmount || 0;

        // 定金 小于 订单金额 - 优惠 - 预付
        var maxDepositAmount = (originalAmount - discountAmount - prepayAmount - depositAmount)
        if (amount > maxDepositAmount) {
            layer.msg("金额需小于" + NP.divide(maxDepositAmount, 100).toFixed(2) + "元");
            value.val("");
            amount = 0;
        }

        // 工程师收款金额 = 订单金额 - 优惠总额 - 预收款 - 定金
        var masterAmount = originalAmount - discountAmount - prepayAmount - depositAmount - amount;
        $(".master-amount").text(NP.divide(masterAmount, 100).toFixed(2));

        // 处理仅上门
        if (masterAmount <= 3000 && updatePriceVO.clean != 2) {
            $(".d-visit").prop("disabled", false);
        } else {
            $(".d-visit").prop("disabled", true).prop("checked", false);
        }
    }

    /**
     * 判断是否是正整数且最多小数点后两位;
     * @param val {number/string} 字符串或数字
     * @returns {boolean} 返回是否是数字
     */
    function isNumber(val) {
        var reg = /^[0-9]+([.]{1}[0-9]{1,2})?$/;//正整数到小数点后两位
        return reg.test(val);
    }

    /**
     * 获取原因
     * @returns {string}
     */
    function getReason() {
        var reason = '';
        $("#reason input:checked").each(function (i) {
            reason += this.value;
            reason += ','
        });
        reason += $('textarea').val();
        return reason;
    }

    /**
     * 保存改价格
     * @param params
     * @returns {boolean}
     */
    function submitUpdatePrice (params) {
        //params['depositAmount'] = NP.times(+params['depositAmount'], 100);
        //params['depositOrderPayId'] = $("#depositAmount").attr("data-th-data-deposit-orderPayId");
        params['amount'] = NP.times(+params['amount'], 100);
        params['orderPayId'] = $("#nonAmount").attr("data-orderPayId");
        params['amountType'] = $("#nonAmount").attr("data-amountType");
        $http.lockSave(SAVE_PRICE_URL, params).then(function () {
            layer.msg('操作成功', { icon: 1, time: 1000 }, function () {
                opCommonUtils.goOperateHome();
            });
        }, (msg) => {
            layer.msg(msg);
        });
        return false;
    }

    /**
     * 初始化页面事件
     */
    function initEvent () {
        // 编辑服务项
        $('.edit-btn').on('click', function () {
            var $this = $(this);

            var workId = $this.data('work-id');
            var orderId = $this.data('order-id');
            var productId = $this.data('product-id');
            var proExtId = $this.data('proext-id');


            layui.layer.open({
                type: 2,
                title:'编辑',
                scrollbar: false,
                content: OMS_BASE_URL + '/work/modify/serviceitem/edit?' + 'workId=' + workId + '&orderId=' + orderId + '&proExtId=' + proExtId + '&productId=' + productId,
                area: ['80%', '80vh'],
                success: function (layero, index) {
                    var iframe = layero.find('iframe')[0];
                    var window = iframe.contentWindow;
                    window.successCallback = function () {
                        layer.close(index);
                        location.reload(true);
                    };
                },
                yes: function () {
                }
            })
        });

        // 监听 定金变化
        $('#depositAmount').on('input', function(){
            depositAmountInput($(this).val());
        });

        // 监听 进度款变化
        $('#nonProgressAmount').on('input', function(){
            progressAmountInput($(this));
        });

        // 监听表单提交
        form.on('submit()', function (data) {
            submitUpdatePrice(data.field);
            return false;
        })
    }

    /**
     * 初始化页面
     */
    function initPage () {
    }



    // 程序入口
    function init () {

      // 初始化页面
      initPage();

      // 初始化事件
      initEvent();

    }


    // 初始化
    init();

    exports('oms_work_modify_serviceitem');
});
